package com.dayaokeji.rhythmschoolstudent.utils;

import android.app.Activity;
import android.support.v7.app.AppCompatDialog;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.App;
import com.dayaokeji.rhythmschoolstudent.client.common.base.Constant;

/**
 * Created by Gorden on 2017/10/17.
 * 签到相关帮助方法
 */

public class SignUtils {

    /**
     * 签到方式： WIFI签到
     */
    public static final int SIGN_WAY_TYPE_WIFI = 1;

    /**
     * 签到方式： wifi + 拍照
     */
    public static final int SIGN_WAY_TYPE_WIFI_PHOTO = 9;

    /**
     * 签到状态
     * @param status
     * @return
     */
    public static String getSignStatus(int status) {
        String statusStr;
        switch (status) {
            case 1:
                statusStr = getStringForRes(R.string.no_sign);
                break;
            case 2:
                statusStr = getStringForRes(R.string.already_sign);
                break;
            case 3:
                statusStr = getStringForRes(R.string.already_leave);
                break;
            case 4:
                statusStr = getStringForRes(R.string.already_late);
                break;
            case 5:
                statusStr = getStringForRes(R.string.already_early);
                break;
            default:
                statusStr = getStringForRes(R.string.unknown);
                break;
        }
        return statusStr;
    }


    private static String getStringForRes(int resId) {
        return App.getInstance().getString(resId);
    }

    public static boolean isExpiredTime(long expiredTime) {
        if (((System.currentTimeMillis()+PrefUtils.getNetTimeDifference()) / 1000) - expiredTime <= Constant.SIGN_QR_CODE_EXPIRED_TIME) {
            return true;
        }else{
            ToastUtils.warning("二维码已经失效");
            return false;
        }
    }

    /**
     * 同步网络时间
     */
    public static void asyncNetTime() {
        TimeUtils.webDateTimeForTimestamp(new TimeUtils.RequestWebDateTimeTimestampListener() {
            @Override
            public void requestDateTime(Long webDateTime) {
                PrefUtils.setNetTimeDifference(webDateTime - System.currentTimeMillis());
            }
        });
    }

    /**
     * 未获取wifi列表提示
     * @param context
     * @return
     */
    public static SpannableStringBuilder getNotFountWifiListTips(Activity context, final View.OnClickListener onClickListener) {
        if (context != null && !context.isFinishing()) {
            final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
            spannableStringBuilder.append("未获取到wifi信息，可能是因为没有wifi权限或是你当前不在签到范围内");
            spannableStringBuilder.append("\r\n\r\n");
            spannableStringBuilder.append("请在状态栏中或应用管理中检查权限WIFI< ");
            final ForegroundColorSpan wifiColorSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.warring_red));
            spannableStringBuilder.setSpan(wifiColorSpan,spannableStringBuilder.length()-6,spannableStringBuilder.length()-2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            final ImageSpan wifiImage = new ImageSpan(context,R.mipmap.ic_wifi,ImageSpan.ALIGN_BASELINE);
            spannableStringBuilder.setSpan(wifiImage,spannableStringBuilder.length()-1,spannableStringBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableStringBuilder.append(">");
            spannableStringBuilder.append("和位置信息< ");
            final ForegroundColorSpan locationColorSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.warring_red));
            spannableStringBuilder.setSpan(locationColorSpan,spannableStringBuilder.length()-6,spannableStringBuilder.length()-2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            final ImageSpan locationImage = new ImageSpan(context, R.mipmap.ic_location,ImageSpan.ALIGN_BASELINE);
            spannableStringBuilder.setSpan(locationImage,spannableStringBuilder.length()-1,spannableStringBuilder.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableStringBuilder.append(">是否开启");
            spannableStringBuilder.append("\r\n\r\n");
            spannableStringBuilder.append("点我，了解如何开启");
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    onClickListener.onClick(widget);
                }
            };
            spannableStringBuilder.setSpan(clickableSpan,spannableStringBuilder.length()-9,spannableStringBuilder.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannableStringBuilder;
        }else{
            return null;
        }
    }

    /**
     * 签到完拍照对话框
     * @param activity
     * @param onClickListener
     */
    public static void showTakePhotoDialog(Activity activity, final View.OnClickListener onClickListener) {
        if (activity != null && !activity.isFinishing()) {
            final AppCompatDialog compatDialog = DialogUtils.getAppCompatDialog(activity, R.layout.dialog_sign_take_photo);
            if (compatDialog != null) {
                compatDialog.findViewById(R.id.btn_take_self_photo).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickListener.onClick(v);
                        compatDialog.dismiss();
                    }
                });

                compatDialog.findViewById(R.id.iv_close_dialog).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (compatDialog != null) {
                            compatDialog.dismiss();
                        }
                    }
                });
            }
        }
    }


}
